/**
 * Created by Administrator on 2018/5/30.
 */
/**
 * Created by Administrator on 2018/1/13.
 */
/**
 * Created by Administrator on 2017/12/2.
 */
define(function(require, exports, module) {
    var $=require('zepto');
    api=require('api');
    cn=require('conment');
    module.exports = {
        int:{
            login:sessionStorage.getItem('testKey'),
            codes:'',
            indexCodes:'',
            time:true,
            zx_astock_list:JSON.parse(cn.getItem('zx_astock')),
        },
        _islogin:function () {
            const _this=this;
            if(_this.int.login) return false
            cn.Ajax(api.isLogin,{
            },function(d){
                if(d.flag==1){
                    sessionStorage.setItem('testKey',true);
                    _this.int.login=true;
                }else{
                    _this.int.login=false;
                }
            },'post')
        },
        indexMarket:function () {
            const _this=this;
            cn.Ajax(api.indexMarket,{
                codes :'000001,399001,399006'
            },function(d){
                var water=new Array();
                // console.log(d)
                var list=d;
                $.each(list,function(i,l,array){
                    var tradePrice=parseFloat(l.tradePrice),preClosePx=parseFloat(l.preClosePx);
                    _this.int.indexCodes+=(l.code+',');
                    water.push('<div class="red" id="indexMar'+l.code+'">' +
                        '<p class="market-name">'+l.stockName+'</p> <h3 class="market-code pri'+l.code+'">'+l.tradePrice+'</h3> ' +
                        '<div class="market-amount"><span class="tra_pre'+l.code+'">'+(tradePrice-preClosePx).toFixed(2)+'</span><span class="f_tra_pre'+l.code+'">'+(((tradePrice-preClosePx)/preClosePx)*100).toFixed(2)+'%</span></div> ' +
                        '</div>');
                });
                $('#indexMarket').append(water.join(''));
                _this.timeIndex_market()

            },'post')
        },
        timeIndex_market:function () {
            const _this=this;
            cn.Ajax(api.indexMarket,{
                codes : _this.int.indexCodes,
            },function(d){
                var list=d;
                $.each(list,function(i,l,array){
                    var tradePrice=parseFloat(l.tradePrice),preClosePx=parseFloat(l.preClosePx);
                    $('.pri'+l.code).text(l.tradePrice);
                    $('.tra_pre'+l.code).text((tradePrice-preClosePx).toFixed())
                    $('.f_tra_pre'+l.code).text((((tradePrice-preClosePx)/preClosePx)*100).toFixed(2)+'%');
                    if(tradePrice>=preClosePx){
                        $('#indexMar'+l.code).addClass('red').removeClass('green');
                    }else{
                        $('#indexMar'+l.code).addClass('green').removeClass('red');
                    }
                });
                setTimeout(function () {
                    _this.timeIndex_market()
                },3000)

            },'post')
        },
        //A股自选
        astock_zx:function(){
            const _this=this;
            const future_list=new Array();
            if(_this.int.zx_astock_list instanceof Array){
                var obj=_this.int.zx_astock_list
            }else{
                localStorage.removeItem('zx_astock');
                var obj=false
            }
            if(obj&&obj.length>0){
                for(var i=0;i<obj.length;i++){
                    future_list.push('<li class="red list'+obj[i]+'" data-code="'+obj[i]+'">' +
                        '<div><p class="hot-stock-name name'+obj[i]+'">--</p><span class="hot-stock-code">'+obj[i]+'</span> </div> ' +
                        '<div class="price price'+obj[i]+'">--</div> ' +
                        '<div class="zfb zfb'+obj[i]+'">--</li>');
                    _this.int.codes +=obj[i]+','
                }
                $('#hotMarketList').append(future_list.join(''));
                $('#hotMarketList li').on('click',function () {
                    window.location.href='/view/astock_market.html?code='+$(this).data('code')
                });
                if(!_this.int.time)return false
                _this.timeOut()
            }else{
                $("#hotMarketList").html('<div class="zwsj" style="padding-bottom: 1.5rem"><img src="../../images/home/kong.png"><p>未添加自选股</p><a href="/view/seach.html">立即添加</a></div>');
                return false
            }
        },

        _hotList:function () {
            const _this=this;
            cn.Ajax(api.url_stock.moreMarket,{
                codes :'000790,002264,600270,601366,002927,000852,000858,603136,600721,002677'
            },function(d){
                // if(d.code==1){
                    var water=new Array();
                    const list=d;
                    $.each(list,function(i,l,array){
                        var tradePrice=parseFloat(l.tradePrice),preClosePx=parseFloat(l.preClosePx);
                        _this.int.codes+=(l.code+',');
                        // console.log(parseFloat((l.tradePrice-l.preClosePx)/l.preClosePx)*100)
                        water.push('<li class="'+(tradePrice>=preClosePx?'red':'green')+' list'+l.code+'" data-code="'+l.code+'">' +
                            '<div><p class="hot-stock-name">'+l.stockName+'</p><span class="hot-stock-code">'+l.code+'</span> </div> ' +
                            '<div class="price price'+l.code+'">'+parseFloat(l.tradePrice).toFixed(2)+'</div> ' +
                            '<div class="zfb zfb'+l.code+'">'+(((tradePrice-preClosePx)/preClosePx)*100).toFixed(2)+'%</div> </li>');
                    });
                    $('#hotMarketList').append(water.join(''));
                    $('#hotMarketList li').on('click',function () {
                        window.location.href='/view/astock_market.html?code='+$(this).data('code')
                    });
                    if(!_this.int.time)return false
                    _this.timeOut()
                // }else{
                //     cn.alert(d.msg)
                // }
            },'post')
        },
        timeOut:function () {
            const _this=this;
            cn.Ajax(api.url_stock.moreMarket,{
                codes : _this.int.codes||'000001',
            },function(d){
                // if(d.code==1){
                    var list=d;
                    $.each(list,function(i,l,array){
                        var tradePrice=parseFloat(l.tradePrice),preClosePx=parseFloat(l.preClosePx);
                        $('.name'+l.code).text(l.stockName);
                        $('.price'+l.code).text(parseFloat(tradePrice==0?l.preClosePx:l.tradePrice).toFixed(2));
                        $('.zfb'+l.code).text(tradePrice==0?"0.00%":((((tradePrice-preClosePx)/preClosePx)*100).toFixed(2)+'%'));
                        if(tradePrice>=preClosePx){
                            $('.list'+l.code).addClass('red').removeClass('green');
                        }else{
                            $('.list'+l.code).addClass('green').removeClass('red');
                        }
                    });
                    setTimeout(function () {

                        _this.timeOut()
                    },3000)
                // }else{
                //     cn.alert(d.msg)
                // }
            },'post')
        },

        msgActive:function (val) {
            const _this=this;
            switch (val){
                case 0:
                    _this._hotList();
                    return tab='热门股票';
                    break;
                case 1:
                    _this.astock_zx();
                    return tab='我的自选';
                    break;

            }
        },

        init:function(){
            const _this=this;
            // _this._hotList();
            //_this.astock_zx();
            _this.msgActive(0);
            _this.indexMarket();
            _this._islogin();
            $("#mores").on('click',function () {
                $('#selectTab').show();
            });
            $('#removeBtn').on('click',function () {
                $('#selectTab').hide();
            });
            $('#tabTxt>div').on('click',function () {
                $('#txtts').text($(this).text());
                var index=$(this).data('index');
                $(this).addClass('tabactive').siblings('div').removeClass('tabactive');
                $('#hotMarketList').html('');
                _this.int.codes='';
                _this.int.time=false;
                _this.msgActive(index);
                $('#selectTab').hide();
            });

            $('#foot_hot_nav .click_login').on('click',function () {
                var href=$(this).data('href');
                if(_this.int.login){
                    window.location.href='/view/'+href+'.html'
                }else{
                    window.location.href='/view/login.html'
                }
            })

        },

    }
});